﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DTO;

namespace DAO
{
   public class ThongKeDAO
    {
       public List<ThongKeDTO> LaySoLuongNhap_Ngay(ThongKeDTO tk)
       {
           List<ThongKeDTO> List = new List<ThongKeDTO>();
           SqlConnection cn = DataProvider.ConnectionData();
           string sql = "sp_ThongKeSoLuongNhap";
           SqlCommand cmd = new SqlCommand(sql,cn);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add("@thoiGian", tk.ThoiGian.ToShortDateString());
           SqlDataReader dr = cmd.ExecuteReader();
           while (dr.Read())
           {
               tk = new ThongKeDTO();
               tk.MaHoa = int.Parse(dr["MaHoa"].ToString());
               tk.TenHoa = dr["TenHoa"].ToString();
               tk.SoLuongNhap = Convert.ToInt32(dr["SoLuong"].ToString());
               List.Add(tk);

           }
           cn.Close();
           return List;
       }


       public List<ThongKeDTO> LaySoLuongBan_Ngay(ThongKeDTO tk)
       {
           List<ThongKeDTO> slHoaBan = new List<ThongKeDTO>();
           SqlConnection cn = DataProvider.ConnectionData();
           string sql = "sp_ThongKeSoLuongBan";
           SqlCommand cmd = new SqlCommand(sql, cn);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add("@thoiGian", tk.ThoiGian.ToShortDateString());
           SqlDataReader dr = cmd.ExecuteReader();
           while (dr.Read())
           {
               tk = new ThongKeDTO();
               tk.MaHoa = int.Parse(dr["MaHoa"].ToString());
               tk.TenHoa = dr["TenHoa"].ToString();
               tk.SoLuongBan = Convert.ToInt32(dr["SoLuong"].ToString());
               slHoaBan.Add(tk);

           }
           cn.Close();
           return slHoaBan;
       }
       public int LaySoLuongDonHang(ThongKeDTO tk)
       {
           int sodonHang;
           SqlConnection cn = DataProvider.ConnectionData();
           string sql = "sp_DemSoLuongDonHangDaBan";
           SqlCommand cmd = new SqlCommand(sql, cn);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add("@SoLuong",SqlDbType.Int);
           cmd.Parameters.Add("@thoiGian", tk.ThoiGian.ToShortDateString());
           cmd.Parameters["@SoLuong"].Direction = ParameterDirection.Output;
           cmd.Parameters["@SoLuong"].Value = 0;
           cmd.ExecuteNonQuery();
           cn.Close();
           sodonHang = Int32.Parse(cmd.Parameters["@SoLuong"].Value.ToString());
           return sodonHang;

       }
       public List<ThongKeDTO> LaySoLuongTonKho(ThongKeDTO tk)
       {
           List<ThongKeDTO> slHoaTon = new List<ThongKeDTO>();
           SqlConnection cn = DataProvider.ConnectionData();
           string sql = "sp_ThongKeSoLuongTonKho";
           SqlCommand cmd = new SqlCommand(sql,cn);
           cmd.CommandType = CommandType.StoredProcedure;
           SqlDataReader dr = cmd.ExecuteReader();
           while (dr.Read())
           {
               tk = new ThongKeDTO();
               tk.MaHoa = int.Parse(dr["MaHoa"].ToString());
               tk.TenHoa = dr["TenHoa"].ToString();
               tk.SoLuongTon = Convert.ToInt32(dr["SoLuongHoa"].ToString());
               slHoaTon.Add(tk);

           }
           cn.Close();
           return slHoaTon;
       }
       public int TinhTongThuNhap(DateTime ngay)
       {
           int thuNhap;
           SqlConnection cn = DataProvider.ConnectionData();
           string sql = "sp_TinhTongThuNhapNgay";
           SqlCommand cmd = new SqlCommand(sql, cn);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add("@thuNhap", SqlDbType.Int);
           cmd.Parameters.Add("@thoiGian", ngay.ToShortDateString());
           cmd.Parameters["@thuNhap"].Direction = ParameterDirection.Output;
           cmd.Parameters["@thuNhap"].Value = 0;
           cmd.ExecuteNonQuery();
           cn.Close();
           thuNhap = Int32.Parse(cmd.Parameters["@thuNhap"].Value.ToString());
           return thuNhap;

       }
    }
}
